﻿using System;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using BikeBouncer.BusinessLayer.Enums;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public class Item : BaseEntity
	{
		#region Properties

		[DataMember]
		public Maker Maker { get; set; }

		[DataMember]
		public User User { get; set; }

		[DataMember]
		public ItemStatus Status { get; set; }

		[DataMember]
		public String Title { get; set; }

		[DataMember]
		public String Details { get; set; }

		[DataMember]
		public String Serial { get; set; }

		[DataMember]
		public String Colors { get; set; }

		[DataMember]
		public DateTime StatusDate { get; set; }

		[DataMember]
		public DateTime RegDate { get; set; }

		[DataMember]
		public DateTime UpdateTime { get; set; }

		#endregion

		#region Extra properties

		public String Key
		{
			get
			{
				return IdToKey(Id);
			}
		}

		public String Code
		{
			get
			{
				return GetCode(Title);
			}
		}

		public String Summary
		{
			get
			{
				return Maker.Title + " - " + Title;
			}
		}

		public String KeyUrl
		{
			get
			{
				return Settings.ItemKeyUrl + Key;
			}
		}

		public String PictureUrl
		{
			get
			{
				if (HasPicture)
				{
					return Settings.ItemPicturesWebPath + Id + ".jpg";
				}

				return Settings.ItemPicturesWebPath + "default.jpg";
			}
		}

		public Boolean HasPicture
		{
			get
			{
				return File.Exists(Settings.ItemPicturesPath + Id + ".jpg");
			}
		}

        #endregion

		#region Constructor

		public Item()
		{
			Maker = new Maker();
			User = new User();
		}

		public Item(Int32 id)
		{
			Construct(DataAccess.Item_Get(id));
		}

		public Item(String key)
		{
			Construct(DataAccess.Item_Get(KeyToId(key)));
		}

		public Item(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Loading

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "ItemId");
			Status = (ItemStatus)DataHandler.GetInt32(reader, "ItemStatusId");
			Title = DataHandler.GetString(reader, "ItemTitle");
			Details = DataHandler.GetString(reader, "ItemDetails");
			Serial = DataHandler.GetString(reader, "ItemSerial");
			Colors = DataHandler.GetString(reader, "ItemColors");
			StatusDate = DataHandler.GetDateTime(reader, "ItemStatusDate");
			RegDate = DataHandler.GetDateTime(reader, "ItemRegDate");
			UpdateTime = DataHandler.GetDateTime(reader, "ItemRegDate");
			Active = DataHandler.GetBoolean(reader, "ItemActive");

			Maker = new Maker();
			Maker.Id = DataHandler.GetInt32(reader, "MakerId");
			Maker.Title = DataHandler.GetString(reader, "MakerTitle");

			if (!String.IsNullOrEmpty(Maker.Title))
			{
				Maker.Load(reader);
			}

			User = new User();
			User.Id = DataHandler.GetInt32(reader, "UserId");
			User.Email = DataHandler.GetString(reader, "UserEmail");

			if (!String.IsNullOrEmpty(User.Email))
			{
				User.Load(reader);
			}
		}

		#endregion

		#region Key methods

		public static String IdToKey(Int32 id)
		{
			String key = id.ToString("X");

			while (key.Length < 6)
			{
				key = "0" + key;
			}

			key = key.Replace("000", "RTY");
			key = key.Replace("00", "GH");

			return key;
		}

		public static Int32 KeyToId(String key)
		{
			key = key.Replace("RTY", "000");
			key = key.Replace("GH", "00");

			try
			{
				return Int32.Parse(key, NumberStyles.HexNumber);
			}
			catch
			{
				return 0;
			}
		}

		#endregion
	}
}